package com.yjps.system.service;


import com.yjps.common.page.Pager;
import com.yjps.system.dao.ResumeDao;
import com.yjps.system.model.Resume;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 麒全 on 2015/8/18.
 */
@Service
public class ResumeService {
    @Autowired
    ResumeDao resumeDao;

    private static final Logger log = LoggerFactory.getLogger(ResumeService.class);


    public List<Resume> getList() {
        String hql = "from Resume r";

        return resumeDao.find(hql);
    }

    public Resume find(int id) {
        return resumeDao.load(id);

    }

    public void add(Resume r) {
        resumeDao.save(r);
    }

    /**
     * 更新简历
     * @param r
     * @return
     */
    public Resume update(Resume r) {
//        resumeDao.update(r);
        String hql = "update Resume r set r.name = :name,r.sex = :sex,r.age = :age,r.job = :job,r.mobile = :mobile,r.email = :email,r.forward = :forward,r.expect_salar = :expect_salar,r.expression = :expression,r.honor = :honor,r.skill = :skill  where r.id = :id";
        Map<String, Object> params = new HashMap<>();
        params.put("name", r.getName());
        params.put("sex", r.isSex());
        params.put("age", r.getAge());
        params.put("job", r.getJob());
        params.put("mobile", r.getMobile());
        params.put("email", r.getEmail());
        params.put("forward", r.getForward());
        params.put("expect_salary", r.getExpect_salar());
        params.put("expression", r.getExpression());
        params.put("honor", r.getHonor());
        params.put("skill", r.getSkill());
        params.put("id", r.getId());
        resumeDao.excute(hql, params);
        log.debug("update the Resume success, id is " + r.getId());
        return resumeDao.load(r.getId());
    }

    public void delete(Resume r) {
//        resumeDao.invalid(r);
        String hql = "update Resume r set r.isValid = 0 where r.id = :id";
        Map<String,Object> parames = new HashMap<>();
        parames.put("id",r.getId());
        resumeDao.excute(hql,parames);
        log.debug("delete the Resume success, id is " + r.getId());

    }

    /**
     * 分页获取简历列表
     * @param pager
     * @return
     */
    public Pager<Resume> getResumeByPager(Pager<Resume> pager){
        Map<String, Object> params = new HashMap<>();
        String queryString = "from Resume t where t.isValid = 1";
        queryString += " order by t.id desc";
        List<Resume> accounts = resumeDao.findWithSelect(queryString, params, (pager.getPageNo()-1)*pager.getPageSize(), pager.getPageSize());
        pager.setCount(resumeDao.getTotalCount(queryString, params));
        pager.setList(accounts);
        return pager;
    }

}
